//Network Assignment client part
//Richard Allsopp
//A Simple Java Client 

//importing java tools
import java.io.*;
import java.net.*;
import java.util.*;

public class networkAssignment
{
	public static void main(String[]args) throws IOException
	{
		Socket mySock = null;				//socket for connection
        PrintWriter out = null;				//printwriter for sending strings
        BufferedReader in = null;			//bufferedReader for reading strings

        try {			//try catch block for catching the can't find server or can't connect to server exception
            mySock = new Socket("UNI-LAPTOP", 40000);		//port number
            out = new PrintWriter(mySock.getOutputStream(), true);		//for sending strings
            in = new BufferedReader(new InputStreamReader(mySock.getInputStream()));	//for reading strings
        } catch (UnknownHostException e) {
            System.err.println("Can't find Jankowski.");		//can't find exception
            System.exit(1);		//close the program if exception
        } catch (IOException e) {
            System.err.println("Can't connect to Jankowski");	//can't connect exception
            System.exit(1);		//close the program if exception
        }
        
		Scanner scan = new Scanner(System.in);
		mainMenu();
		while(scan.hasNext())
		{	
			int input = scan.nextInt();
			if(input == 1)
			{
				menuLog();
			}
			if(input == 2)
			{
				searchOps();
			}
			if(input == 3)
			{
				buy();
			}
			if(input == 5)
			{
				login();
			}
			if(input == 6)
			{
				loginRegister();
			}
			if(input == 4)
			{
				break;
			}
		}
		
        out.close();
        in.close();
        mySock.close();
        //closing all the connections	
	}
	public static void mainMenu()
	{
		System.out.println();
		System.out.println("Test book system");
		System.out.println();
		System.out.println("1. login");
		System.out.println("2. Browse");
		System.out.println("2. Buy Books");
		System.out.println("4. Disconnect");
	}
	public static void menuLog()
	{
		System.out.println("Login/Register");
		System.out.println();
		System.out.println("5. login");
		System.out.println("6. Register");
	}
	public static void searchOps()
	{
		System.out.println("Search Books");
	}
	public static void login()
	{
		//Protocol variables
		Scanner scanLog = new Scanner(System.in);
		System.out.println("Enter your name:");
		String logDet = scanLog.next();
		String message = "0|L|"+logDet+"|%";
		System.out.println(message);
		mainMenu();
	}
	public static void loginRegister()
	{
		//Protocol variables
		Scanner scanLogReg = new Scanner(System.in);
		System.out.println("Enter your name:");
		String logDetReg = scanLogReg.next();
		System.out.println("Enter your address:");
		String logDetRegAdd = scanLogReg.next();
		String message = "0|R|"+logDetReg+"|"+logDetRegAdd+"|%";
		System.out.println(message);
		mainMenu();
	}
	public static void buy()
	{
		System.out.println("Book ID");
	}	
}


